﻿using DapperExtensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interface.Dapper
{
    public class DapperSort
    {
        public bool Ascending { get; set; }

        public string Field { get; set; }

        public DapperSort(string field, bool ascending)
        {
            Field = field;
            Ascending = ascending;
        }

        public static List<DapperSort> SortAsc(string field)
        {
            List<DapperSort> sorts = new List<DapperSort>();

            sorts.Add(new DapperSort(field, true));

            return sorts;
        }

        public static List<DapperSort> SortDesc(string field)
        {
            List<DapperSort> sorts = new List<DapperSort>();

            sorts.Add(new DapperSort(field, false));

            return sorts;
        }

        public static List<DapperSort> SortMultiple(Dictionary<string, bool> dic)
        {
            List<DapperSort> sorts = new List<DapperSort>();

            foreach (var item in dic)
            {
                sorts.Add(new DapperSort(item.Key, item.Value));
            }

            return sorts;
        }

        public static IList<ISort> ConvertSort(List<DapperSort> sorts)
        {
            if (sorts == null) { return null; }

            IList<ISort> list = new List<ISort>();

            sorts.ForEach(m => { list.Add(new TempSort(m.Field, m.Ascending)); });

            return list;
        }
    }

    public class TempSort : ISort
    {
        public bool Ascending { get; set; }

        public string PropertyName { get; set; }

        public TempSort(string propertyName, bool ascending)
        {
            PropertyName = propertyName;
            Ascending = ascending;
        }

    }
}